package io.lckgroup.modules.lck.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.net.URLEncoder;

/**
 * @author huangrui
 */
public class GetPayPage {

    //获取pay链接
    public static String getpaypage(String merchantNo, String storeNo, String posNum, String userAgent, String outTradeNo, String successUrl, BigDecimal totalAmount) {
        String ok;
        String temp = "";
        //拼接url
        StringBuilder url = new StringBuilder("https://lzwxsd.lzccb.cn/payGateway/scan/tf/getOAuthUrl?");
        try {
            url.append("merchantNo=");
            url.append(merchantNo);
            url.append("&storeNo=");
            url.append(storeNo);
            url.append("&posNum=");
            url.append(posNum);
            url.append("&userAgent=");
            url.append(userAgent);
            url.append("&outTradeNo=");
            url.append(outTradeNo);
            url.append("&totalAmount=");
            url.append(totalAmount);
//            url.append("&merchantNoticeUrl=");
//            url.append("http://203.195.199.244:8081/dc/mp/callback/callback");
            url.append("&successUrl=");
            url.append(URLEncoder.encode(successUrl, "utf-8"));//解决前端url参数丢失
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpPost httpPost = new HttpPost(url.toString());//构建一个POST请求
            HttpResponse response = client.execute(httpPost);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println("到返回的HttpResponse的\"实体:" + content);
            String[] split = content.split("\"");
            for (String s : split) {
                String trim = s.trim();
                boolean b = trim.startsWith("https");
                if (b) {
                    temp = trim;
//                    System.out.println(temp);
//                    String[] split1 = temp.split("=http");
//                    String uu = split1[0]+"=https"+split1[1];
//                    System.out.println("修改后的url为："+uu);
//                    temp = uu;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }
}
